package com.wink.file.center.service;

import com.aliyun.oss.OSS;
import com.wink.file.center.config.AliyunOSSConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * aliyunoss服务impl
 *
 * @author xw
 * @date 2020-11-17 17:55
 */
@Slf4j
@Service("aliyunOssServiceImpl")
public class AliYunOssServiceImpl implements FileService {

    @Autowired
    private OSS ossClient;

    @SneakyThrows
    @Override
    public Map<String, String> upload(MultipartFile file, String fileName) {
        fileName = createFileName(file,fileName);
        ossClient.putObject(AliyunOSSConfig.bucketName, fileName, file.getInputStream());

        Map<String, String> map = new HashMap<>(2);
        map.put("url", AliyunOSSConfig.domain  + fileName);
        log.warn("aliYun OSS:{}", AliyunOSSConfig.domain + fileName);
        return map;
    }

    @Override
    public void delete(String id) {

    }
}
